import moment from 'moment'
import _ from 'lodash'
import store from '@/store'

export const momentJsMap = {
    '==': (time1, time2) => {
        return moment(time1).isSame(time2, 'day')
    },
    '!=': (time1, time2) => {
        return !moment(time1).isSame(time2, 'day')
    },
    '>': (time1, time2) => {
        return moment(time1).isAfter(time2, 'day')
    },
    '>=': (time1, time2) => {
        return moment(time1).isSameOrAfter(time2, 'day')
    },
    '<': (time1, time2) => {
        return moment(time1).isBefore(time2, 'day')
    },
    '<=': (time1, time2) => {
        return moment(time1).isSameOrBefore(time2, 'day')
    },
    'is null': (time1) => {
        return !time1
    },
}

export const momentJsRangeMap = {
    '==': (time, timeList) => {
        return _.some(timeList, (item) => {
            return moment(time).isBetween(item.startTime, item.endTime, 'day', '[]')
        })
    },
    '!=': (time, timeList) => {
        return _.some(timeList, (item) => {
            return !moment(time).isBetween(item.startTime, item.endTime, 'day', '[]')
        })
    },
    '>': (time, timeList) => {
        return _.some(timeList, (item) => {
            return moment(time).isAfter(item.endTime, 'day')
        })
    },
    '>=': (time, timeList) => {
        return _.some(timeList, (item) => {
            return moment(time).isSameOrAfter(item.endTime, 'day')
        })
    },
    '<': (time, timeList) => {
        return _.some(timeList, (item) => {
            return moment(time).isBefore(item.startTime, 'day')
        })
    },
    '<=': (time, timeList) => {
        return _.some(timeList, (item) => {
            return moment(time).isSameOrBefore(item.startTime, 'day')
        })
    },
    'is null': (time1) => {
        return !time1
    },
}


/**
 * @description: Determine whether to display components through conditional rules
 * @param {String,Number} value: value that need to be compared
 * @param {Object} format: conditional rules for components
 * */
export const conditionFunction = (value, format) => {
    const rules = format.dataForm.filters.rules
    const condition = format.dataForm.filters.condition
    const evalArray = rules.map(rule => {
        if (rule.opt === 'is null') {
            return value === null
        }
        if (rule.type === 'date' || rule.type === 'datetime') {
            value = moment.tz(value, store.state.user.timezoneId).format('YYYY-MM-DD')
            // eslint-disable-next-line no-prototype-builtins
            if (rule.hasOwnProperty('valueList')) {
                return `momentJsRangeMap['${rule.opt}']('${value}',  ${JSON.stringify(rule['valueList'])})`
            } else {
                return `momentJsMap['${rule.opt}']('${value}',  ${JSON.stringify(rule['value'])})`
            }
        } else {
            if (rule.opt === '==' || rule.opt === '!=') {
                return `("${(value).toString()}" ${rule.opt}  "${(rule.value).toString()}")`
            } else {
                return `("${Number(value)}"  ${rule.opt}  "${Number(rule.value)}")`
            }
        }
    })
    const evalString = evalArray.join(condition === 'and' ? '&&' : '||')
    return eval(evalString)
}

/***
 *
 * @description: Determine whether to display components through conditional rules
 * @param {Array} data: data that need to be compared
 * @param {Object} format: conditional rules for components
 * */

export const conditionData = (data, format) => {
    return data.map(da => {
        try {
            let evalString = ''
            let formatStyle = ''
            format.map(item => {
                const rules = item.dataForm.filters.rules
                const condition = item.dataForm.filters.condition
                formatStyle = {
                    textColor: item.textColor,
                    backgroundColor: item.backgroundColor,
                }
                const evalArray = rules.map(rule => {
                    let value = da[rule.key]
                    if (rule.type === 'date' || rule.type === 'datetime') {
                        value = moment.tz(value, store.state.user.timezoneId).format('YYYY-MM-DD')
                        // eslint-disable-next-line no-prototype-builtins
                        if (rule.hasOwnProperty('valueList')) {
                            return `momentJsRangeMap['${rule.opt}']('${value}',  ${JSON.stringify(rule['valueList'])})`
                        } else {
                            return `momentJsMap['${rule.opt}']('${value}',  ${JSON.stringify(rule['value'])})`
                        }
                    } else {
                        if (rule.opt === '==' || rule.opt === '!=') {
                            return `("${(value).toString()}" ${rule.opt}  "${(rule.value).toString()}")`
                        } else {
                            return `(${Number(value)}  ${rule.opt}  ${Number(rule.value)})`
                        }
                    }
                })
                evalString = evalArray.join(condition === 'and' ? '&&' : '||')
                if (eval(evalString) && !da.format) {
                    da.format = formatStyle
                }
            })
            return da
        } catch (e) {
            console.log(e)
        }
    })
}